<template>
  <el-dialog
    :title="showTitle"
    :visible="dialogVisible"
    @close="handleClose"
  >
    <el-form
      ref="roleDialogForm"
      label-width="80px"
      :model="roleForm"
      :rules="rules"
    >
      <el-form-item
        prop="name"
        label="角色"
      >
        <el-input v-model="roleForm.name" />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="roleForm.description" type="textarea" row="3" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button v-loading="loading" type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addRoleAPI, updateRoleAPI } from '@/api/setting'
export default {
  name: 'RoleDialog',
  components: {},
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleForm: {
        // name: '',
        // description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称必填', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  computed: {
    showTitle() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  watch: {},
  created() {},
  methods: {
    handleClose() {
      this.$emit('update:dialogVisible', false)
      this.$refs.roleDialogForm.resetFields()
      this.roleForm = {}
    },
    async handleSubmit() {
      try {
        await this.$refs.roleDialogForm.validate()
        this.loading = true
        this.roleForm.id ? await updateRoleAPI(this.roleForm) : await addRoleAPI(this.roleForm)
        this.$message.success(`角色${this.roleForm.id ? '编辑' : '新增'}成功`)
        this.handleClose()
        this.$parent.getRoleList()
      } catch (error) {
        console.log(error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>

</style>
